package com.klun.project.dal.common.interceptor;

import java.sql.Connection;
import java.sql.Statement;
import java.util.Properties;

import com.klun.project.log.Constants;
import com.klun.project.log.KLog;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Plugin;
import org.apache.ibatis.plugin.Signature;

@Intercepts({@Signature(
		type = StatementHandler.class,
		method = "update",
		args = {Statement.class}
)})
public class UpdateInterceptor implements Interceptor, Constants {
	private static KLog LOGGER = KLog.getLogger(UpdateInterceptor.class);

	public UpdateInterceptor() {
	}

	public Object intercept(Invocation invocation) throws Throwable {
//		Tracer buildTracer = LOGGER.buildTracer();
//		Span span = buildTracer.createEmpty();
		String sql = null;
		String url = null;
		Object inputData = null;
		String method = "update";

		StatementHandler statementHandler;
		try {
			statementHandler = (StatementHandler) invocation.getTarget();
			sql = statementHandler.getBoundSql().getSql();
			char startChar = sql.charAt(0);
			startChar = Character.toLowerCase(startChar);
			switch (startChar) {
				case 'd':
					method = "delete";
					break;
				case 'i':
					method = "insert";
			}

			Statement statement = (Statement) invocation.getArgs()[0];
			Connection connection = statement.getConnection();
			url = QueryInterceptor.getUrl(connection.getMetaData().getURL());
//			span = buildTracer.createSpan("SQL", method + ".sql");
//			span.addEvent("SQL.Method", method);
			inputData = statementHandler.getParameterHandler().getParameterObject();
			if (LOGGER.isDebugEnabled()) {
				String reflectionToString = "" + inputData;
				LOGGER.debug("JDBC_IN {} URL {} PARAM {}", new Object[]{sql, url, reflectionToString});
			}
		} catch (Throwable var21) {
			LOGGER.error("failed to create cat transaction", var21);
		}

		statementHandler = null;
		boolean var19 = false;

		Object var24;
		try {
			var19 = true;
			Object object = invocation.proceed();
//			span.success();
			var24 = object;
			var19 = false;
		} catch (Throwable var20) {
			String reflectionToString = "" + inputData;
			LOGGER.error("JDBC_EX {} URL {} PARAM {}", new Object[]{sql, url, reflectionToString});
//			span.failed(var20);
			throw var20;
		} finally {
			if (var19) {
//				span.addEvent("SQL.Database", url);
//				long var14 = span.getDurationInMillis();
//				if (var14 > QueryInterceptor.LONG_SQL) {
//					span.addData("sql", sql);
//					LOGGER.warn("JDBC_LONG {}", sql);
//				}
//
//				span.close();
			}
		}

//		span.addEvent("SQL.Database", url);
//		long durationInMillis = span.getDurationInMillis();
//		if (durationInMillis > QueryInterceptor.LONG_SQL) {
//			span.addData("sql", sql);
//			LOGGER.warn("JDBC_LONG {}", sql);
//		}
//
//		span.close();
		return var24;
	}

	public Object plugin(Object target) {
		return Plugin.wrap(target, this);
	}

	public void setProperties(Properties properties) {
	}
}